import streamlit as st
from tools.verification import *
from time import sleep
from tools.DBUtil import DBUtil
import pandas as pd

@st.dialog("新增配置")
def create_group():
    groupname = st.text_input("组别名称", key="create_group_name")
    admin_email = st.text_input("管理邮箱", key="create_group_admin")

    create_group = st.button("创建",key="create_group_button")
    create_group_message = st.empty()

    if create_group:
        if groupname and admin_email:
            if is_valid_group_exist(groupname):
                create_group_message.error("组别名称已存在,请更换名称")
                return
            # 写入数据库
            DBUtil.uid_db("INSERT INTO config_group (groupname, adminemail) VALUES (%s, %s)",(groupname, admin_email))
            create_group_message.success("新增组别配置成功")
            sleep(1)
            st.rerun()
        else:
            create_group_message.error("请填写完整信息")

@st.dialog("删除配置")
def delete_group():
    group_options = [""]
    group_result = DBUtil.select_all("SELECT groupname FROM config_group;")
    if len(group_result) != 0:
        group_options = [group['groupname'] for group in group_result]
    result = st.multiselect("选择组别", group_options, placeholder="")
    delete_group_button = st.button("删除",key="delete_group_button")
    if delete_group_button:
        if result:
            for group in result:
                DBUtil.uid_db("DELETE FROM config_group WHERE groupname = %s",(group,))
            st.success("删除组别成功!")
            sleep(1)
            st.rerun()
        else:
            st.warning("请选择组别")

@st.dialog("修改配置")
def modify_group():
    group_options = [""]
    group_result = DBUtil.select_all("SELECT groupname FROM config_group;")
    if len(group_result) != 0:
        group_options = group_options + [group['groupname'] for group in group_result]

    groupname = st.selectbox("选择组别", group_options, index=0, key="modify_group_select")
    if groupname:
        # 读取数据库信息
        group_result = DBUtil.select_one("SELECT * FROM config_group WHERE groupname = %s",(groupname,))
        admin_email = group_result['adminemail']

        # 显示表单
        new_groupname = st.text_input("更新组别名称",groupname, key="modify_group_name")
        admin_email = st.text_input("管理邮箱",admin_email, key="modify_group_admin")

        modify_group_button = st.button("修改",key="modify_group_button")
        modify_group_message = st.empty()

        if modify_group_button:
            if new_groupname and admin_email:
                # 更新数据库
                DBUtil.uid_db("UPDATE config_group SET groupname=%s, adminemail=%s WHERE groupname=%s",(new_groupname, admin_email, groupname))
                modify_group_message.success("修改组别信息成功")
                sleep(1)
                st.rerun()
            else:
                modify_group_message.error("请填写完整信息")
    else:
        st.warning("请选择组别")

# ==================================================页面内容控制========================================================
st.title("组别信息管理")
col1,col2,col3,col4 = st.columns([1,1,1,15])

if col1.button("新增", key="create_group"):
    create_group()  # 调用创建组别函数

if col2.button("删除",key="delete_group"):
    delete_group()  # 调用删除组别函数

if col3.button("修改",key="modify_group"):
    modify_group()  # 调用修改组别函数

data = DBUtil.select_all("SELECT * FROM config_group;")
if len(data) == 0:
    st.warning("暂无数据")
else:
    df = pd.DataFrame(data)
    df.columns = ['组别名称','管理邮箱']
    df.set_index('组别名称', inplace=True)
    st.dataframe(df)  # 显示虚拟机列表